<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>cors</title>
</head>

<body>
  <div>cors跨域</div>
  <script>
    var xhr = new XMLHttpRequest(); // IE8/9需用window.XDomainRequest兼容
    // 前端设置是否带cookie
    xhr.withCredentials = true;
    document.cookie = "cook=chan";
    xhr.open('get', 'http://localhost:4000/getData', true);
    xhr.setRequestHeader('name','chan')
    // xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) {
        console.log(xhr.responseText);
console.log(xhr.getResponseHeader('name'));
      } else if (xhr.readyState == 304) {
        onsole.log(xhr.responseText);
      }
    };
    xhr.send();
  </script>
</body>

</html>